package com.example.gorannet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class TicketsActivity extends Activity implements OnItemClickListener {

    private AlertDialogManager alert             = new AlertDialogManager();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mychoicelist);

        ListView listView = (ListView)findViewById(R.id.lv11);
        List<String> listData = new ArrayList<String>();
        DBHandler dbHandler = new DBHandler(getApplicationContext());
        listData = dbHandler.getTickets();
        listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, R.id.listItem, listData));
        listView.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;
    }

    String ticket = "";
    String     nPid[] = new String[1];
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        try{
            String pid = ((TextView)arg1.findViewById(R.id.listItem)).getText().toString();
            nPid = new String[1];
            nPid = pid.split(" ");
            ticket = nPid[0];
            if(ticket.equals("Empty")){
            	alert.showAlertDialog(TicketsActivity.this, "Warning",
	                    "No Ticket Found!", false);
            }
            else{
                Intent i = new Intent(getApplicationContext(), TicketDetailsActivity.class);
                i.putExtra("TICKET", ticket);
                startActivityForResult(i, 0);
            }
        }
        catch(Exception e){
            alert.showAlertDialog(getApplicationContext(), "Warning",
                    "No Ticket Found!", false);
        }

    }

}